﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace ConfigInfoWebPlatform.API.BusinessCommon
{
    public class ReturnInfo
    {
        public Code Code { get; set; }

        public string Msg { get; set; }

        [JsonIgnore]
        public bool Succeeded => Code.Equals(Code.Success);

        [JsonIgnore]
        public bool Unsuccessful => !Succeeded;

        public static ReturnInfo Fail()
        {
            return new ReturnInfo
            {
                Code = Code.Fail
            };
        }

        public static ReturnInfo Fail(string message)
        {
            return new ReturnInfo
            {
                Code = Code.Fail,
                Msg = message
            };
        }

        public static ReturnInfo<T> Fail<T>(string message, T data = default(T))
        {
            return new ReturnInfo<T>
            {
                Data = data,
                Code = Code.Fail,
                Msg = message
            };
        }

        public static ReturnInfo Success(string message = "") => new ReturnInfo
        {
            Code = Code.Success,
            Msg = message
        };

        public static ReturnInfo<T> Success<T>(T data, string message = "")
        {
            return new ReturnInfo<T>
            {
                Data = data,
                Code = Code.Success,
                Msg = message
            };
        }

        public static ReturnInfo Return(bool result)
        {
            return result
                       ? Success()
                       : Fail();
        }

        public static ReturnInfo Return(bool result, string successMessage, string failMessage)
        {
            return result
                ? Success(successMessage)
                : Fail(failMessage);
        }

        public static ReturnInfo<T> Return<T>(bool result, T data)
        {
            return Return(result, data, string.Empty, string.Empty);
        }

        public static ReturnInfo<T> Return<T>(bool result, T data, string successMessage, string failMessage)
        {
            return result ? Success(data, successMessage) : Fail<T>(failMessage);
        }
    }

    /// <summary>
    /// 通用返回对象
    /// </summary>
    /// <typeparam name="T">指定类型</typeparam>
    public class ReturnInfo<T> : ReturnInfo
    {
        public ReturnInfo()
        {

        }

        public ReturnInfo(ReturnInfo returnInfo)
        {
            if (returnInfo == null)
                throw new ArgumentNullException(nameof(returnInfo));
            Code = returnInfo.Code;
            Msg = returnInfo.Msg;
        }

        /// <summary>
        /// 返回的具体数据
        /// </summary>
        public T Data { get; set; }
    }

    public enum Code
    {
        /// <summary>
        /// 操作失败
        /// </summary>
        [Description("操作失败")]
        Fail = -1,
        [Description("操作成功")]
        Success = 0
    }
}
